package service 
{
	import cn.inswf.framework.mvcs.Actor;
	import data.LMS;
	import flash.external.ExternalInterface;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author liwei
	 */
	public class AiccService extends Actor 
	{
		[Inject]
		public var _localService:LocalSaveService;
		public function AiccService() 
		{
			super();
			if (ExternalInterface.available) {
				ExternalInterface.addCallback("save", send);
			}
			
		}
		private function getTime():String {
			var s:int = (getTimer() - State.startTime)/1000;
			var HH:int = int(s/3600);
			var MM:int = int((s-HH*3600)/60);
			var SS:int = int(s-HH*3600-MM*60);
			return (fill0(HH.toString(),-1)+":"+fill0(MM.toString(),-1)+":"+fill0(SS.toString(),-1));
		}
		public function fill0(x:String, n:int):String
		{
			var left:Boolean = n < 0;
			n = n < 0? -n:n;
			var s:String = "";
			for (var i:int = 0; i < (n - x.length);i++ ) {
				s += '0';
			}
			if (left) {
				return s + x;
			}else {
				return x + s;
			}
		}
		public function send():void {
			LMS.encode();
			_localService.save();
			if (ExternalInterface.available) {
				ExternalInterface.call(Config.JSname, "aicc_set", LMS.aiccData);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.core.lesson_location;" + LMS.location);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.core.lesson_status;" + LMS.status);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.core.lesson_progress;" + LMS.evaluation);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.core.score.raw;" + LMS.score);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.evaluation;" + LMS.evaluation);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.core.session_time;" + LMS.time);
				ExternalInterface.call(Config.JSname, "sco_set", "cmi.suspend_data;" + LMS.scormData);
			}
		}
	}

}